package com.liang.kborcl.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.env.EnvironmentPostProcessor;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.PropertiesPropertySource;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.Properties;

@Component
@Slf4j
public class ShardingsphereConfiguration implements EnvironmentPostProcessor {

    public static final String SHARDINGSPHERE_ENABLED = "spring.shardingsphere.enabled";

    public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
        try {
            String property = environment.getProperty(SHARDINGSPHERE_ENABLED);
            if (!StringUtils.isEmpty(property)) {
                return;
            }
            // 修改为默认关闭，需要手动配置开启
            log.warn("The configuration [{}] has been modified to be [false] by default. If you need to open it, please configure it [true] manually",SHARDINGSPHERE_ENABLED);
            Properties properties = new Properties();
            properties.setProperty(SHARDINGSPHERE_ENABLED, "false");
            PropertiesPropertySource propertySource = new PropertiesPropertySource("shardingsphere", properties);
            environment.getPropertySources().addLast(propertySource);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}